package com.blq.developtools.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blq.developtools.R;
import com.blq.developtools.bean.PropertyBean;
import com.blq.developtools.bean.ViewTreeBean;
import com.blq.developtools.interfaces.OnAdapterLinkClickListener;
import com.blq.developtools.interfaces.OnLayoutStructureSelectListener;
import com.blq.developtools.utils.DevelopUtils;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import blq.ssnb.snbview.listener.OnIntervalClickListener;

/**
 * <pre>
 * ****************************************************************
 * 文件名称:ViewPropertiesAdapter.java
 * 作 者:SSNB
 * 创建时间:2024/5/22
 * 文件描述:
 * 注意事项:
 * 版权声明:Copyright (C) 2015-2025 杭州中焯信息技术股份有限公司
 * 修改历史:2024/5/22 1.00 初始版本
 * ****************************************************************
 * </pre>
 */
public class ViewPropertiesAdapter extends RecyclerView.Adapter<ViewPropertiesAdapter.ViewPropertiesViewHolder> {
    private List<PropertyBean> mData;
    private ViewTreeBean showView;
    private Context context;

    private OnAdapterLinkClickListener<ViewTreeBean> listener;

    public ViewPropertiesAdapter(Context context) {
        this.context = context;
        mData = new ArrayList<>();
    }

    @NonNull
    @Override
    public ViewPropertiesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewPropertiesViewHolder holder;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view_property, parent, false);
        holder = new ViewPropertiesViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewPropertiesViewHolder holder, int position) {
        PropertyBean bean = mData.get(position);
        holder.updateInfo(bean.getTitle(), bean.getValue(), bean.getLinkView(), listener);
    }


    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setListener(OnAdapterLinkClickListener<ViewTreeBean> listener) {
        this.listener = listener;
    }

    public void setData(ViewTreeBean viewTreeBean) {
        if (showView != viewTreeBean) {
            showView = viewTreeBean;
            analysisProperties(showView);
            notifyDataSetChanged();
        }
    }

    private void analysisProperties(ViewTreeBean showView) {
        mData.clear();
        mData.add(generateStringPropertyBean("View ID Name", DevelopUtils.unNullBean(showView.getViewIDName(), "无")));
        mData.add(generateStringPropertyBean("Class Type", DevelopUtils.unNullBean(showView.getClassType(), "未知")));
        mData.add(generateStringPropertyBean("Text", DevelopUtils.unNullBean(showView.getText(), "")));
        mData.add(generateStringPropertyBean("宽度", DevelopUtils.FormatPxDp(context, showView.getWidth())));
        mData.add(generateStringPropertyBean("高度", DevelopUtils.FormatPxDp(context, showView.getHeight())));
        ViewTreeBean parent = showView.getParentView();
        String parentName = getViewName(parent);
        mData.add(generateLinkPropertyBean("Parent View", parentName, parent));
        if (showView.getChildView().isEmpty()) {
            mData.add(generateLinkPropertyBean("Child View", "无", null));
        } else {
            mData.add(generateStringPropertyBean("Child View", showView.getChildView().size() + "个children"));
            for (ViewTreeBean bean : showView.getChildView()) {
                mData.add(generateLinkPropertyBean("", getViewName(bean), bean));
            }
        }
        mData.add(generateStringPropertyBean("应用包名", showView.getPackageName()));
        mData.add(generateStringPropertyBean("相对屏幕坐标", DevelopUtils.FormatLocation(context, showView.getInScreenLocation())));
        mData.add(generateStringPropertyBean("相对Parent坐标", DevelopUtils.FormatLocation(context, showView.getInParentLocation())));

        mData.add(generateStringPropertyBean("层级", showView.getLayerLevel() + ""));
        mData.add(generateStringPropertyBean("在ParentView中层级", showView.getInParentLevel() + ""));
        mData.add(generateStringPropertyBean("content Description", showView.getContentDescription()));
    }

    private PropertyBean generateStringPropertyBean(String title, String value) {
        PropertyBean bean = new PropertyBean();
        bean.setTitle(title);
        bean.setValue(value);
        return bean;
    }

    private PropertyBean generateLinkPropertyBean(String title, String value, ViewTreeBean treeBean) {
        PropertyBean bean = new PropertyBean();
        bean.setTitle(title);
        bean.setValue(value);
        bean.setLinkView(treeBean);
        return bean;
    }

    private static String getViewName(ViewTreeBean bean) {
        String parentName = "";
        if (bean != null) {
            String id = DevelopUtils.FormatResourceName(bean.getViewIDName());
            parentName = bean.getClassType();
            if (!id.isEmpty()) {
                parentName += "(" + DevelopUtils.FormatResourceName(bean.getViewIDName()) + ")";
            }
        }
        return parentName;
    }

    public static class ViewPropertiesViewHolder extends RecyclerView.ViewHolder {
        protected TextView titleView;
        protected TextView valueView;

        public ViewPropertiesViewHolder(@NonNull View itemView) {
            super(itemView);
            titleView = itemView.findViewById(R.id.tv_title);
            valueView = itemView.findViewById(R.id.tv_value);
        }

        public void updateInfo(String title, String value, ViewTreeBean bean, OnAdapterLinkClickListener<ViewTreeBean> listener) {
            titleView.setText(title);
            valueView.setText(value);
            if (bean == null) {
                valueView.getPaint().setUnderlineText(false);
                valueView.setOnClickListener(null);
            } else {
                valueView.getPaint().setUnderlineText(true);
                valueView.setOnClickListener(new OnIntervalClickListener() {
                    @Override
                    public void onEffectiveClick(View v) {
                        if (listener != null) {
                            listener.onLinkClick(bean);
                        }
                    }
                });
            }
        }
    }
}
